// -----------------------------------------------------------------------
// Eros Fratini - eros@recoding.it
// jqprint 0.3
//
// - 19/06/2009 - some new implementations, added Opera support
// - 11/05/2009 - first sketch
//
// Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
// requires jQuery 1.3.x
//
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
//------------------------------------------------------------------------

(function($) {
    var opt;
    $.fn.jqprint = function (options) {
        opt = $.extend({}, $.fn.jqprint.defaults, options);

        var $element = (this instanceof jQuery) ? this : $(this);
        var doc;
        var usetab = false;
        if ($.browser.opera || ($.browser.msie))// && ($.browser.version < 8)))
        {
            usetab = true;
            var tab = window.open('','webcash','height=1000,width=800,scrollbars=1,resizable=1');//window.open('','webcash-preview','height=1,width=1');
            doc = tab.document;
        }
        else 
        {
            var $iframe = $("<iframe  />");
        
            if (!opt.debug) {
                $iframe.css({
                    position: "absolute",
                    width: "0px",
                    height: "0px",
                    left: "-600px",
                    top: "-600px"
                });
            }


            $iframe.appendTo("body");
            doc = $iframe[0].contentWindow.document;
        }
        doc.open();
        doc.write('<html><head>');
        if (opt.importCSS)
        {
            if ($("link[media='print']").length > 0)
            {
                $("link[media='print']").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
                });
            }
            else 
            {
                $("link[type='text/css']").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
        }
        doc.write('</head><body>');
        if(opt.printcount == null || !(opt.printcount instanceof Number))
            opt.printcount = 1;
        for(var i = 1; i <= opt.printcount; i++){

            if(i > 1 && opt.uselinebreak){
                doc.write('<div style="margin: 1px; height: 1px; border-bottom: 1px dashed #000000"></div>');
            }
            var container = $('<div></div>');

            var clone = $element.clone();
            if(opt.onBeforePrint != null && opt.onBeforePrint instanceof Function){
                opt.onBeforePrint(clone, i);
            }
            doc.write((container.html(clone).html()));
            if(opt.usepagebreak && i < opt.printcount)
                doc.write('<div style="page-break-before:always;"></div>');
        }
        doc.write('</body></html>');
        doc.close();
        
        try{
            if(usetab){
                tab.focus();
                tab.print();
                //tab.close();
            }else{
                $iframe[0].contentWindow.print();
                setTimeout(function(){$iframe.remove();}, 30000);
            }
        }catch(ex){
            alert("Trình duyệt bạn đang dùng không hỗ trợ lệnh in, \nbạn nên chuyển sang trình duyệt như FireFox bản mới nhất");
        }
    }
    
    $.fn.jqprint.defaults = {
        debug: false,
        importCSS: true,
        uselinebreak: false,
        usepagebreak: false,
        printcount: 1,
        onBeforePrint: function(){} /* tham so truyen vao la, bien jquery object, thu tu in */
    };

    // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
    jQuery.fn.outer = function() {
        return $($('<div></div>').html(this.clone())).html();
    }

    $.fn.jqprintmanypages = function (options) {
        opt = $.extend({}, $.fn.jqprintmanypages.defaults, options);

        var $element = (this instanceof jQuery) ? this : $(this);
        var doc;
        var usetab = false;
        if ($.browser.opera || ($.browser.msie && ($.browser.version < 8)))
        {
            usetab = true;
            var tab = window.open('','name','height=1,width=1');//window.open('','webcash-preview','height=1,width=1');
            doc = tab.document;
        }
        else
        {
            var $iframe = $("<iframe  />");
            $iframe.css({
                position: "absolute",
                width: "0px",
                height: "0px",
                left: "-600px",
                top: "-600px"
            });

            $iframe.appendTo("body");
            doc = $iframe[0].contentWindow.document;
        }
        doc.open();
        doc.write('<html><head>');
        if (opt.importCSS)
        {
            if ($("link[media='print']").length > 0)
            {
                $("link[media='print']").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
                });
            }
            else
            {
                $("link[type='text/css']").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
        }
        doc.write('</head><body>');
        for(var i = 0; i < $element.length; i++){

            if(i > 0 && opt.uselinebreak){
                doc.write('<div style="margin: 1px; height: 1px; border-bottom: 1px dashed #000000"></div>');
            }
            var container = $('<div></div>');
            var clone = $element.eq(i).clone();
            doc.write((container.html(clone).html()));
            if(opt.usepagebreak && i < $element.length - 1)
                doc.write('<div style="page-break-before:always;"></div>');
        }
        doc.write('</body></html>');
        doc.close();
        try{
            if(usetab){
                tab.focus();
                tab.print();
                tab.close();
            }else{
                $iframe[0].contentWindow.print();
            }
        }catch(ex){
            alert("Trình duyệt bạn đang dùng không hỗ trợ lệnh in, \nbạn nên chuyển sang trình duyệt như IE, FireFox, Chrome bản mới nhất");
        }
    }
    $.fn.jqprintmanypages.defaults = {
        importCSS: true,
        uselinebreak: false,
        usepagebreak: false,
        onBeforePrint: function(){}
    };
})(jQuery);